package com.search_wheel.mapper;

import com.search_wheel.pojo.entity.Content;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface ContentMapper {

    /**
     * 根据关键词搜索内容
     * @param keyword 搜索关键词
     * @param offset 偏移量
     * @param limit 限制条数
     * @return 内容列表
     */
    @Select("<script>" +
            "SELECT * FROM t_content WHERE status = 1 " +
            "AND (title LIKE CONCAT('%', #{keyword}, '%') " +
            "OR summary LIKE CONCAT('%', #{keyword}, '%')) " +
            "<choose>" +
            "   <when test='sort == \"time\"'>" +
            "       ORDER BY create_time DESC" +
            "   </when>" +
            "   <otherwise>" +
            "       ORDER BY (title LIKE CONCAT('%', #{keyword}, '%')) DESC, " +
            "                (summary LIKE CONCAT('%', #{keyword}, '%')) DESC" +
            "   </otherwise>" +
            "</choose>" +
            " LIMIT #{offset}, #{limit}" +
            "</script>")
    List<Content> searchByKeyword(@Param("keyword") String keyword,
                                  @Param("offset") int offset,
                                  @Param("limit") int limit,
                                  @Param("sort") String sort); // 新增sort参数

    /**
     * 统计搜索结果数量
     */
    @Select("SELECT COUNT(*) FROM t_content WHERE status = 1 " +
            "AND (title LIKE CONCAT('%', #{keyword}, '%') " +
            "OR summary LIKE CONCAT('%', #{keyword}, '%'))")
    int countByKeyword(@Param("keyword") String keyword);

    /**
     * 根据作者ID搜索内容
     */
    @Select("SELECT * FROM t_content WHERE author_id = #{authorId} AND status = 1 " +
            "ORDER BY create_time DESC LIMIT #{offset}, #{limit}")
    List<Content> findByAuthorId(@Param("authorId") Integer authorId,
                                 @Param("offset") int offset,
                                 @Param("limit") int limit);

    /**
     * 根据ID查询单个内容
     */
    @Select("SELECT * FROM t_content WHERE id = #{id}")
    Content findById(@Param("id") Integer id);

    /**
     * 插入新内容
     */
    @Insert("INSERT INTO t_content(title, content, summary, author_id, category_id, " +
            "status, view_count, like_count, create_time, update_time) " +
            "VALUES(#{title}, #{content}, #{summary}, #{authorId}, #{categoryId}, " +
            "#{status}, #{viewCount}, #{likeCount}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Content content);

    /**
     * 更新内容
     */
    @Update("UPDATE t_content SET title = #{title}, content = #{content}, " +
            "summary = #{summary}, category_id = #{categoryId}, status = #{status}, " +
            "update_time = #{updateTime} WHERE id = #{id}")
    int update(Content content);

    /**
     * 增加浏览量
     */
    @Update("UPDATE t_content SET view_count = view_count + 1 WHERE id = #{id}")
    int incrementViewCount(@Param("id") Integer id);

    /**
     * 增加点赞量
     */
    @Update("UPDATE t_content SET like_count = like_count + 1 WHERE id = #{id}")
    int incrementLikeCount(@Param("id") Integer id);

    /**
     * 根据状态查询内容列表
     */
    @Select("SELECT * FROM t_content WHERE status = #{status} " +
            "ORDER BY create_time DESC LIMIT #{offset}, #{limit}")
    List<Content> findByStatus(@Param("status") Short status,
                               @Param("offset") int offset,
                               @Param("limit") int limit);

    @Select("SELECT title FROM t_content " +
            "WHERE title LIKE CONCAT('%', #{keyword}, '%') " +
            "LIMIT #{limit}")
    List<String> findTitlesContaining(@Param("keyword") String keyword,
                                      @Param("limit") int limit);

    @Select("SELECT title FROM t_content WHERE status = 1")
    List<String> findAllTitles();
}
